<template>
  <el-drawer title="字典分类管理" :visible.sync="drawer" :wrapperClosable="false" ref="drawer"
    size="700px" :before-close="handleDrawerClose" class="wpwork-common-drawer" append-to-body>
    <div class="wpwork-flex-main">
      <div class="wpwork-common-head">
        <topOpts @refresh="getDictionaryTypeList()" @add="addOrUpdateHandle()">
          <upload-btn url="/api/system/DictionaryData/Actions/Import" accept=".bdd"
            @on-success="getDictionaryTypeList" />
        </topOpts>
        <div class="wpwork-common-head-right">
          <el-tooltip effect="dark" :content="$t('common.refresh')" placement="top">
            <el-link icon="icon-ym icon-ym-Refresh
              wpwork-common-head-icon" :underline="false" @click="reset()" />
          </el-tooltip>
        </div>
      </div>
      <wpwork-table v-loading="listLoading" :data="tableData" row-key="id" default-expand-all
        :tree-props="{children: 'children', hasChildren: ''}">
        <el-table-column prop="fullName" label="名称" />
        <el-table-column prop="enCode" label="编码" />
        <el-table-column prop="sortCode" label="排序" width="70" align="center" />
        <el-table-column prop="isTree" label="是否树" width="60" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.isTree === 1" type="success" size="mini">是</el-tag>
            <el-tag v-else type="danger" size="mini">否</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130">
          <template slot-scope="scope" v-if="scope.row.id!='0'&&scope.row.id!='1'">
            <tableOpts @edit="addOrUpdateHandle(scope.row.id)" @del="handleDel(scope.row.id)">
              <el-button size="mini" type="text" @click="exportData(scope.row.id)">导出
              </el-button>
            </tableOpts>
          </template>
        </el-table-column>
      </wpwork-table>
      <TypeForm v-if="typeFormVisible" ref="TypeForm" @refreshDataList="getDictionaryTypeList" />
    </div>
  </el-drawer>
</template>
<script>
import {
  getDictionaryType,
  delDictionaryType,
  exportData
} from '@/api/systemData/dictionary'
import TypeForm from './Form'

export default {
  components: {
    TypeForm
  },
  data() {
    return {
      drawer: false,
      listLoading: false,
      btnLoading: false,
      typeFormVisible: false,
      tableData: [],
      exampleData: [{
        "id": "0",
        "parentId": null,
        "hasChildren": null,
        "isTree": null,
        "children": [],
        "fullName": "事件大类3",
        "enCode": null,
        "sortCode": 0,
        "category": null
      },
      {
        "id": "1",
        "parentId": null,
        "hasChildren": null,
        "isTree": null,
        "children": [],
        "fullName": "事件大类2",
        "enCode": null,
        "sortCode": 0,
        "category": null
      },
      {
        "id": "2",
        "parentId": null,
        "hasChildren": true,
        "isTree": null,
        "children": [{
          "id": "443409825091648261",
          "parentId": "1",
          "hasChildren": true,
          "isTree": 0,
          "children": [{
            "id": "443670211682369477",
            "parentId": "443409825091648261",
            "hasChildren": false,
            "isTree": 0,
            "children": null,
            "fullName": "事件1",
            "enCode": "msgNoticeType",
            "sortCode": 0,
            "category": "1"
          },
          ],
          "fullName": "事件小类1.1",
          "enCode": "msgCenter",
          "sortCode": 0,
          "category": "系统"
        },
        {
          "id": "821ddb80b4f14b918f32549f72a81473",
          "parentId": "1",
          "hasChildren": true,
          "isTree": 0,
          "children": [{
            "id": "038d293fca544c86afa3f2bc65421fd7",
            "parentId": "821ddb80b4f14b918f32549f72a81473",
            "hasChildren": false,
            "isTree": 1,
            "children": null,
            "fullName": "事件2",
            "enCode": "kehuleixing",
            "sortCode": 0,
            "category": "1"
          },
          {
            "id": "3e2d33e54d554b4b8a33fc41b93f8a2d",
            "parentId": "821ddb80b4f14b918f32549f72a81473",
            "hasChildren": false,
            "isTree": 0,
            "children": null,
            "fullName": "事件3",
            "enCode": "shenpi",
            "sortCode": 0,
            "category": "1"
          }
          ],
          "fullName": "事件小类1.2",
          "enCode": "FunctionExample",
          "sortCode": 0,
          "category": "系统"
        },
        ],
        "fullName": "事件大类1",
        "enCode": null,
        "sortCode": 0,
        "category": null
      }
      ],
    }
  },
  methods: {
    init() {
      this.drawer = true
      this.$nextTick(() => {
        this.getDictionaryTypeList()
      })
    },
    getDictionaryTypeList() {
      this.listLoading = true
      // getDictionaryType().then(res => {
      this.tableData = this.exampleData;
      this.listLoading = false
      // }).catch(() => {
      //   this.listLoading = false
      // })
    },
    addOrUpdateHandle(id) {
      this.typeFormVisible = true
      this.$nextTick(() => {
        this.$refs.TypeForm.init(id)
      })
    },
    handleDel(id) {
      this.$confirm(this.$t('common.delTip'), this.$t('common.tipTitle'), {
        type: 'warning'
      }).then(() => {
        delDictionaryType(id).then(res => {
          this.$message({
            type: 'success',
            message: res.msg,
            duration: 1500,
            onClose: () => {
              this.$store.commit('base/SET_DICTIONARY_LIST', [])
              this.getDictionaryTypeList()
            }
          })
        })
      }).catch(() => { })
    },
    reset() {
      this.getDictionaryTypeList()
    },
    handleDrawerClose(done) {
      done();
      this.$emit('refreshDataList')
    },
    exportData(id) {
      this.$confirm('您确定要导出该字典分类, 是否继续?', '提示', {
        type: 'warning'
      }).then(() => {
        exportData(id).then(res => {
          this.wpwork.downloadFile(res.data.url)
        })
      }).catch(() => { });
    }
  }
}
</script>
